home *** CD-ROM | disk | FTP | other *** search
- /* LogFormat.c */
- /*
- * LogFormat.c
- * Copyright © 1992-94 Apple Computer Inc. All Rights Reserved.
- * Programmed by Martin Minow,
- * Internet: minow@apple.com
- * AppleLink: MINOW
- *
- * Edit History
- * 93.01.09 MM First public release
- * 93.07.09 MM Reformatted for 80-column page. No substantive changes.
- * 94.12.10 MM Redone (form AuditEntryFormat) for the Driver Log.
- *
- * Format a LogRecordEntry. Note: there is no size checking, which means that if you
- * somehow manage to create an extra-long record, you'll get garbage. The local
- * formatting functions won't corrupt memory, however. result may not be an expression
- * with side-effects.
- */
- #include "LogLibrary.h"
-
- #define AppendChar(result, theChar) do { \
- StringPtr _dst = (result); \
- _dst[++_dst[0] & 0xFF] = theChar; \
- } while (0)
- static void AppendUnsignedLeadingZeros(
- StringPtr result,
- unsigned long value,
- short digits,
- short terminator
-
- );
- static void AppendSigned(
- StringPtr result,
- signed long value
- );
- static void AppendUnsigned(
- StringPtr result,
- unsigned long value
- );
- static void AppendHexLeadingZeros(
- StringPtr result,
- unsigned long value,
- short digits
- );
- static void AppendOSType(
- StringPtr result,
- OSType datum
- );
- static void AppendPascalString(
- StringPtr result,
- const StringPtr datum
- );
-
- enum {
- NUL = 0,
- kOpenQuote = 0xD2,
- kCloseQuote = 0xD3
- };
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * FormatLogEntryData
- *
- * This function is in LogFormat.c - it formats an entry into a single line
- * that is stored in the result. Note: only the data is formatted: the timestamp
- * is not processed. This function may be called from a non-application context;
- * It does not require a MixedMode switch.
- */
- pascal void
- FormatLogEntryData(
- const LogEntryPtr thisLogEntry, /* Format this entry */
- StringPtr result
- )
- {
- UInt32 datum;
- UInt32 format;
- unsigned int thisFormat;
- register UInt32 *dataPtr;
- #define ENTRY (*thisLogEntry)
-
- result[0] = 0;
- AppendOSType(result, ENTRY.idCode);
- AppendPascalString(result, "\p: ");
- format = ENTRY.format;
- dataPtr = ENTRY.data;
- while (dataPtr < &ENTRY.data[kLogEntryDataSize]) {
- thisFormat = (format & kLogFormatMask);
- format >>= kLogFormatShift;
- if (thisFormat == kLogFormatEnd)
- break;
- else if (thisFormat == kLogFormatString) {
- if (dataPtr > &ENTRY.data[0])
- AppendChar(result, ' ');
- AppendChar(result, kOpenQuote); /* Open double quote */
- AppendPascalString(result, (StringPtr) dataPtr);
- AppendChar(result, kCloseQuote);
- break; /* Exit loop */
- }
- else {
- if (dataPtr > &ENTRY.data[0])
- AppendPascalString(result, "\p, ");
- datum = *dataPtr++;
- switch (thisFormat) {
- case kLogFormatSigned:
- AppendSigned(result, (signed long) datum);
- break;
- case kLogFormatUnsigned:
- AppendUnsigned(result, datum);
- break;
- case kLogFormatHex:
- AppendHexLeadingZeros(result, datum, sizeof (UInt32) * 2);
- AppendPascalString(result, "\p=");
- AppendOSType(result, datum);
- break;
- case kLogFormatAddress:
- default: /* Unknown */
- AppendHexLeadingZeros(result, datum, sizeof (UInt32) * 2);
- break;
- }
- }
- }
- #undef ENTRY
- }
-
- /*
- * Output an n-digit decimal value with leading zeros.
- */
- static void
- AppendUnsignedLeadingZeros(
- StringPtr result,
- unsigned long value,
- short digits,
- short terminator
- )
- {
- if (--digits > 0)
- AppendUnsignedLeadingZeros(result, value / 10, digits, NUL);
- AppendChar(result, (value % 10) + '0');
- if (terminator != NUL)
- AppendChar(result, terminator);
- }
-
- /*
- * Output a signed decimal longword.
- */
- static void
- AppendSigned(
- StringPtr result,
- signed long value
- )
- {
- if (value < 0) {
- AppendChar(result, '-');
- value = (-value);
- }
- AppendUnsigned(result, (unsigned long) value);
- }
-
- /*
- * Output an unsigned decimal longword.
- */
- static void
- AppendUnsigned(
- StringPtr result,
- unsigned long value
- )
- {
- if (value >= 10)
- AppendUnsigned(result, value / 10);
- AppendChar(result, (value % 10) + '0');
- }
-
- /*
- * Output a string of hex digits with leading zeros.
- */
- static void
- AppendHexLeadingZeros(
- StringPtr result,
- unsigned long value,
- short digits
- )
- {
- if (--digits > 0)
- AppendHexLeadingZeros(result, value >> 4, digits);
- value &= 0x0F;
- AppendChar(result,
- (value < 10)
- ? value + '0'
- : (value + ('A' - 10))
- );
- }
-
- /*
- * Output a 4-byte character string. Unknown
- * bytes (characters outside the range ' ' to '~')
- * are replaced as '.'.
- */
- static void
- AppendOSType(
- StringPtr result,
- OSType datum
- )
- {
- register short i;
- register unsigned char c;
- union {
- OSType osType;
- unsigned char c[sizeof (OSType)];
- } value;
-
- AppendChar(result, '\'');
- value.osType = datum;
- for (i = 0; i < sizeof (OSType); i++) {
- c = value.c[i];
- if (c < ' ' || c >= 0x7F)
- c = '.';
- AppendChar(result, c);
- }
- AppendChar(result, '\'');
- }
-
- static void
- AppendPascalString(
- StringPtr result,
- const StringPtr datum
- )
- {
- register short i;
-
- for (i = 1; i <= datum[0]; i++)
- AppendChar(result, datum[i]);
- }
-